package game.craft.model;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.image.ImageObserver;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: ccayirog
 * Date: Mar 26, 2011
 * Time: 11:54:38 PM
 * To change this template use File | Settings | File Templates.
 */
public class Craft extends AbstractItem implements Item {

    private int dx;
    private int dy;
    private List<Item> missiles;

    private final int CRAFT_SIZE = 20;

    public Craft() {
        super("D:\\Documents and Settings\\ccayirog\\Desktop\\my_game\\images\\craft.png", 40, 60);
        missiles = new ArrayList<Item>();
    }


    public void move() {
        x += dx;
        y += dy;
        for (Item missile : missiles) {
            if (missile.isExist())
                missile.move();
        }
    }


    public void keyPressed(KeyEvent e) {

        int key = e.getKeyCode();

        if (key == KeyEvent.VK_SPACE) {
            fire();
        }

        if (key == KeyEvent.VK_LEFT) {
            dx = -2;
        }

        if (key == KeyEvent.VK_RIGHT) {
            dx = 2;
        }

        if (key == KeyEvent.VK_UP) {
            dy = -2;
        }

        if (key == KeyEvent.VK_DOWN) {
            dy = 2;
        }
    }

    public void keyReleased(KeyEvent e) {
        int key = e.getKeyCode();

        if (key == KeyEvent.VK_LEFT) {
            dx = 0;
        }

        if (key == KeyEvent.VK_RIGHT) {
            dx = 0;
        }

        if (key == KeyEvent.VK_UP) {
            dy = 0;
        }

        if (key == KeyEvent.VK_DOWN) {
            dy = 0;
        }
    }

    public void fire() {
        missiles.add(new Missile(x + CRAFT_SIZE, y + CRAFT_SIZE / 2));
    }

    public List<Item> getMissiles() {
        return missiles;
    }

    public void draw(Graphics graphics, ImageObserver observer) {
        if (exist) {
            graphics.drawImage(image, x, y, observer);
            for (Item missile : missiles) {
                missile.draw(graphics, observer);
            }
        }
    }
}
